package org.example.client8000.controller;

import org.example.SystemDemo.comment.enity.*;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import oshi.SystemInfo;
import oshi.hardware.CentralProcessor;
import oshi.hardware.GlobalMemory;
import oshi.hardware.NetworkIF;

import java.io.File;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping(value = "/DynamicInfo")
public class DynamicInfoController {
    /**
     * 网络信息
     */
    @RequestMapping(value = "/getDynamicNet",method = RequestMethod.GET)
    public ArrayList<Net> getDynamicNet() throws InterruptedException {
        SystemInfo systemInfo = new SystemInfo();
        ArrayList<Net> nets = new ArrayList<>();
        NetworkIF[] networkIFs = systemInfo.getHardware().getNetworkIFs().toArray(new NetworkIF[0]);
        for (int i = 0; i < networkIFs.length; i++) {
            Net net = new Net();
            net.setName(networkIFs[i].getName());
            net.setDisplayName(networkIFs[i].getDisplayName());

            long re1 = networkIFs[i].getBytesRecv();
            long sent1 = networkIFs[i].getBytesSent();
            TimeUnit.SECONDS.sleep(1);
            NetworkIF[] networkIFs2 = systemInfo.getHardware().getNetworkIFs().toArray(new NetworkIF[0]);
            long re2 = networkIFs2[i].getBytesRecv();
            long sent2 = networkIFs2[i].getBytesSent();
            net.setNetRecv(formatByte(re2-re1));
            net.setNetSent(formatByte(sent2-sent1));

            nets.add(net);
        }

        return nets;
    }

    /**
     * 内存信息
     */
    @RequestMapping(value = "/getDynamicMemory",method = RequestMethod.GET)
    public Memory getDynamicMemory() {
        SystemInfo systemInfo = new SystemInfo();
        Memory info = new Memory();
        GlobalMemory memory = systemInfo.getHardware().getMemory();
        //总内存
        long totalByte = memory.getTotal();
        //剩余
        long acaliableByte = memory.getAvailable();
        info.setTotalMemory(formatByte(totalByte));
        info.setFreeMemory(formatByte(acaliableByte));
        info.setUsedMemory(formatByte(totalByte-acaliableByte));
        info.setMemoryUsage(new DecimalFormat("#.##%").format((totalByte-acaliableByte)*1.0/totalByte));
        return info;
    }

    /**
     * cpu信息
     */
    @RequestMapping(value = "/getDynamicCPU",method = RequestMethod.GET)
    public CPU getDynamicCPU() throws InterruptedException {
        SystemInfo systemInfo = new SystemInfo();
        CPU info = new CPU();
        CentralProcessor processor = systemInfo.getHardware().getProcessor();
        long[] prevTicks = processor.getSystemCpuLoadTicks();
        // 睡眠1s
        TimeUnit.SECONDS.sleep(1);
        long[] ticks = processor.getSystemCpuLoadTicks();
        long nice = ticks[CentralProcessor.TickType.NICE.getIndex()] - prevTicks[CentralProcessor.TickType.NICE.getIndex()];
        long irq = ticks[CentralProcessor.TickType.IRQ.getIndex()] - prevTicks[CentralProcessor.TickType.IRQ.getIndex()];
        long softirq = ticks[CentralProcessor.TickType.SOFTIRQ.getIndex()] - prevTicks[CentralProcessor.TickType.SOFTIRQ.getIndex()];
        long steal = ticks[CentralProcessor.TickType.STEAL.getIndex()] - prevTicks[CentralProcessor.TickType.STEAL.getIndex()];
        long cSys = ticks[CentralProcessor.TickType.SYSTEM.getIndex()] - prevTicks[CentralProcessor.TickType.SYSTEM.getIndex()];
        long user = ticks[CentralProcessor.TickType.USER.getIndex()] - prevTicks[CentralProcessor.TickType.USER.getIndex()];
        long iowait = ticks[CentralProcessor.TickType.IOWAIT.getIndex()] - prevTicks[CentralProcessor.TickType.IOWAIT.getIndex()];
        long idle = ticks[CentralProcessor.TickType.IDLE.getIndex()] - prevTicks[CentralProcessor.TickType.IDLE.getIndex()];
        long totalCpu = user + nice + cSys + idle + iowait + irq + softirq + steal;
        info.setCpuSysUsage(new DecimalFormat("#.##%").format(cSys * 1.0 / totalCpu));
        info.setCpuUserUsage(new DecimalFormat("#.##%").format(user * 1.0 / totalCpu));
        info.setCpuCurrentUsage(new DecimalFormat("#.##%").format(1.0-(idle * 1.0 / totalCpu)));
        return info;
    }
    /**
     * 磁盘信息
     */
    @RequestMapping(value = "/getDynamicDisk",method = RequestMethod.GET)
    public ArrayList<Disk> getDynamicDisk() {
        SystemInfo systemInfo = new SystemInfo();
        ArrayList<Disk> disks = new ArrayList<>();

        File[] files = File.listRoots();
        for (File file : files) {
            Disk disk = new Disk();
            disk.setTotal(formatByte(file.getTotalSpace()));
            disk.setFree(formatByte(file.getFreeSpace()));
            disk.setUsed(formatByte(file.getTotalSpace() - file.getFreeSpace()));
            disk.setUsage(new DecimalFormat("#.##%").format((file.getTotalSpace() * 1.0 - file.getFreeSpace())/file.getTotalSpace()));
            disk.setPath(file.getPath());
            disks.add(disk);
        }
        return disks;
    }



    public static FormatByteInfo formatByte(long byteNumber){
        //换算单位
        double FORMAT = 1024.0;
        double kbNumber = byteNumber/FORMAT;
        DecimalFormat df = new DecimalFormat("0.00");
        if(kbNumber<FORMAT){
            return new FormatByteInfo(new Double(df.format(kbNumber)),"KB");
        }
        double mbNumber = kbNumber/FORMAT;
        if(mbNumber<FORMAT){
            return new FormatByteInfo(new Double(df.format(mbNumber)),"MB");
        }
        double gbNumber = mbNumber/FORMAT;
        if(gbNumber<FORMAT){
            return new FormatByteInfo(new Double(df.format(gbNumber)),"GB");
        }
        double tbNumber = gbNumber/FORMAT;
        return new FormatByteInfo(new Double(df.format(tbNumber)),"TB");
    }

}


